package cn.edu.nju.oraldefense.action;

import java.util.Calendar;
import java.util.HashMap;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;

import cn.edu.nju.oraldefense.model.Comment;
import cn.edu.nju.oraldefense.model.Paper;
import cn.edu.nju.oraldefense.model.Teacher;
import cn.edu.nju.oraldefense.service.CommentService;
import cn.edu.nju.oraldefense.service.PaperService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CommentAddAction extends ActionSupport {

	private int paperId;
	// private int teacherId;//teacher应该直接从session中获取
	private String pass;
	private Comment comment;
	private CommentService commentService;
	private PaperService paperService;
	private boolean success;

	public int getPaperId() {
		return paperId;
	}

	public void setPaperId(int paperId) {
		this.paperId = paperId;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	@JSON(serialize = false)
	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}

	@JSON(serialize = false)
	public CommentService getCommentService() {
		return commentService;
	}

	@Resource
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	@JSON(serialize = false)
	public PaperService getPaperService() {
		return paperService;
	}

	@Resource
	public void setPaperService(PaperService paperService) {
		this.paperService = paperService;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String execute() {
		HashMap map = new HashMap();
		map.put("yes", 1);
		map.put("no", 2);

		if ((!pass.equals("yes") )&& (!pass.equals("no"))) {
			System.out.println(false);
			return SUCCESS;
		}
		System.out.println("paper");
		// comment.content从客户端填写
		// comment.paper
		Paper paper = paperService.getById(paperId);
		paper.setState((Integer) map.get(pass));
		paperService.modifyPaper(paper);
		comment.setPaper(paper);
		// comment.teacher
		ActionContext ctx = ActionContext.getContext();
		Teacher teacher = (Teacher) ctx.getSession().get("teacher");
		comment.setTeacher(teacher);
		// comment.time
		comment.setTime(Calendar.getInstance().getTime());
		
		commentService.addComment(comment);

		success = true;
		return SUCCESS;
	}

}
